package com.pkutech.carcasher.content;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.pkutech.carcasher.R;
import com.pkutech.carcasher.model.SysMsg;
import com.pkutech.carcasher.utils.CommonUtils;
import com.shizhefei.mvc.IDataAdapter;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by cyy on 2016/6/19.
 */
public class SysMsgAdapter extends BaseAdapter implements IDataAdapter<List<SysMsg>> {

    private Context context;
    private List<SysMsg> sysMsgList = new ArrayList<SysMsg>();
    private LayoutInflater inflater;

    public SysMsgAdapter(Context context) {
        super();
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return sysMsgList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = inflater.inflate(R.layout.sys_msg_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        SysMsg sysMsg = sysMsgList.get(position);
        holder.msgTitle.setText(sysMsg.getTitle());
        try {
            holder.msgTime.setText(CommonUtils.getPublishTimeString(CommonUtils.parseDateFromString2(sysMsg.getCreatedate())));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        holder.msgContent.setText(sysMsg.getContext());
//        convertView.setTag(R.string.order_detail_item_id, selectBookingOrder);
        return convertView;
    }

    @Override
    public void notifyDataChanged(List<SysMsg> data, boolean isRefresh) {
        if (isRefresh) {
            sysMsgList.clear();
        }
        sysMsgList.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public List<SysMsg> getData() {
        return sysMsgList;
    }

    static class ViewHolder {
        @BindView(R.id.msgTitle)
        TextView msgTitle;
        @BindView(R.id.msgTime)
        TextView msgTime;
        @BindView(R.id.msgContent)
        TextView msgContent;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

}
